package com.jason.www.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jason.www.pojo.User;
import com.jason.www.dao.UserMapper;
import com.jason.www.pojo.vo.UserVo;
import com.jason.www.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JasonChoi
 * @since 2018-06-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Override
    @Cacheable(value = "userCache" ,key = "'findUserByEmailLike:email:'+#email")
    public List<User> findUserByEmailLike(String email) {
        logger.info("findUserByEmailLike 方法被调用。。。。");
        EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
        userEntityWrapper.like(User.USER_NAME,"%"+email+"%");
        List<User> users = this.selectList(userEntityWrapper);
        return users;
    }

    @Override
    public UserVo findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    @CacheEvict(value = "userCache",allEntries=true)
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }


    /**
     * 清除userCache所有key内容
     */
    @CacheEvict(value = "userCache",allEntries=true)
    public void reloadUserCache() { }
}
